{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**问题：**\n",
    "假设你正在为一个电影推荐系统设计一个简单的KNN算法。我们有以下一些用户的电影评分数据，数据由两个特征组成：用户对电影A和电影B的评分，分别在1-5之间。用户的标签（电影类型偏好）是动作片（标签0）或者是喜剧片（标签1）。我们有一个新用户，他给电影A评分为3，电影B评分为4。请问这个用户可能偏好哪种类型的电影？\n",
    "\n",
    "**数据：**\n",
    "\n",
    "| 用户   | 电影A评分 | 电影B评分 | 偏好类型 |\n",
    "| ------ | --------- | --------- | -------- |\n",
    "| 用户1  | 5         | 1         | 动作片   |\n",
    "| 用户2  | 4         | 2         | 动作片   |\n",
    "| 用户3  | 2         | 5         | 喜剧片   |\n",
    "| 用户4  | 1         | 4         | 喜剧片   |\n",
    "| 用户5  | 3         | 2         | 动作片   |\n",
    "| 用户6  | 2         | 5         | 喜剧片   |\n",
    "\n",
    "你需要做以下步骤：\n",
    "1. 构造数据\n",
    "2. 创建KNN模型\n",
    "3. 使用数据训练模型\n",
    "4. 预测新用户的喜好"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 0. 引入核心包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.neighbors import KNeighborsClassifier"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. X, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.array([[5,1],[4,2],[2,5],[1,4],[3,2],[2,5]])\n",
    "\n",
    "y = np.array([0,0,1,1,0,1]) # 0表示动作片，1表示喜剧片"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 创建 KNN 模型\n",
    "k = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "knn = KNeighborsClassifier(n_neighbors=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3. 训练模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "KNeighborsClassifier(n_neighbors=1)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "knn.fit(X, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4. 用模型推理(预测)用户的喜好"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_user = np.array([[3, 4]])\n",
    "prediction = knn.predict(new_user)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5. 数据可视化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAY0AAAEYCAYAAACgDKohAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjQuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/MnkTPAAAACXBIWXMAAAsTAAALEwEAmpwYAAAx30lEQVR4nO3deZyVZf3/8dfHAVkcFL8BAwgxmVrJqkMKasK4EOECEZqGudV3cqGvuRKZqZXlL5VvmiGR4lJTo5kL4lbhoIBiMoKAUcoXQUdcsNiGTRg+vz/ue8bD4cyZ+8zMPefMzPv5eNwP7uW6r/tzLg7nw71dl7k7IiIiUeyT7QBERKTlUNIQEZHIlDRERCQyJQ0REYlMSUNERCJT0hARkciUNNo4M5trZg/XsW2Rmd3XzCElHv8GM3Mze7OO7SvD7TfEdOyPmqCewjDGmmlz2K5nNkWc4THGmtkKM/vYzFY3Vb0iqShpSK7bDnzGzIYmrjSzLwL9wu1xuBv4chPWdxUwHPga8CbwoJmd2th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qE9x9a9TjSfNQ0mjj3L3KzI4HfgbcTHDd/wOCyzfXevBOAcBKgksZPyO4/LOO4BHcHyTU9QszW0twCelKguf+36COx1jj4O5/MbOzCB43nUhwKeY2gmSSrAz4LbDQ3VfXU+92MysmeIroRoKzsA8JHhWd1WQfYM9jzg//bm4keLw5jyCpP0P994UuJWj7/w5j3kRwo/qeCMdtyr/HS8KYvw18n6DNas9+3P1BM9tE8D26kOApt1UE362P96pNss7cNdyriIhEo6enREQkMiUNERGJTElDREQiU9KQepnZgWa2wYJeUL9uwZgQ281sm5n9MsL+d5vZ7rD8tpoO68zs82a2Lu74RaTptLgb4d26dfPCwsIG7btlyxb222+/+gs2s1yNC4LY3n//fdydQw45hI0bNwJwwAEHsGXLFv75z38yYMAAOnToUGcdq1evZsuWLfTv33+vbcuXL6dHjx706JH8Pl79ceVimymuzCiuzDQmroqKio/cvXujg8h251eZTkVFRd5Q5eXlDd43Trkal3sQW35+vs+bNy/l9o4dO/pf/vKXtHV861vf8oEDB6bcNmXKFO/fv3+D4spFiisziiszjYkLWOTqsFDitnXrVrZu3cpxxx2317Z7772X3bt3U1xcnGLPPb3++ut06tSJPn368PLLL9euHz9+PCtXrmzSmEUkPkoaklZlZSXt2u39DuiSJUv4zne+w69//euU2xNNnjyZ9evXs23bNo4//nhGjx5du+3www/n44/1DpdIS6E3wiWtLl26UF1dvce6yspKjjnmGCZNmsS3v11njxS1Dj300Nr5++67b4/7Hxs2bGDPMXtEJJfpTEPS6tUr6EV7w4YNAFRVVTFo0CC+/OUvM3Xq1D3KDh8+nMmTJ+9Vx5IlS2rnr7vuuj1u5D333HMceOCBTR+4iMRCSUPqdfDBBzNjxgwArr76atavX88zzzxDp06d6NSpEw8+GHRJtGrVKj772c/utf/FF19Mx44d6dSpEzNmzOChhz7pG7CsrIyRI0c2y+cQkcaLNWmY2WozW2ZmS8xsr7EELHCHma0MB6c/Ms54JLpLnryEdj9uR8V7FawcvJKfTv0pAHfddRfuzrZt22qnr389GMuourqak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PPZXtqERaHCUNaTv22w9uuw1efBH23x/uvFP9V4lkqNnf0xDJuqOPhldfDcboCDtA5OWX4cwzg2URqVObO9PYtm0bI0aMoDp8a3j06NF07dqVU089tZ499/Twww9jZixatAiAdevWMXr06CaPV2LSoQN0C3us+eUv4ayzgsd0165tdNWJ37E1a9ZQVFTEkCFD6N+/P9OnT49cj75jkovaXNKYOXMm48ePJy8vD4Crr76a3/3udxnVsXnzZu644w6OPvro2nXdu3enV69eLFiwoEnjlWZw663B9OyzwVvkd9/dqMtWid+xXr168eKLL7JkyRJefvllbr75ZtZGSEz6jkmuanNJo7S0lLFjx9Yun3jiiXTp0iWjOq677jquueYaOnbsuMf6cePGUVpa2iRxSjNq1w6uvDK4UT5kCPz3fwdnHw2U+B3bd9996dChAwA7duxg9+7dkerQd0xyVZtKGjt37mTVqlUUFhY2uI7FixfzzjvvpLycNXToUObNm9eICCWrDjkEnnsOZs6Eb30rWPfuuxl1gJjqO/bOO+8waNAg+vbty+TJk+ndu3faOvQdk1zWppLGxo0b6dq1a4P33717N5dffjm33XZbyu09evSIdOlBctg++wQ95u6//ycdIB5zDCxfHmn3VN+xvn37snTpUlauXMn999/PBx98UOf++o5JrmtTSaNDhw5s3769wftv3ryZ5cuXM3LkSAoLC1m4cCGnn3567Y3K7du306lTp6YKV7KtXTu49lpYtQqOPBJuvBE+/jjtLum+Y71796Z///5pzxT0HZNc16aSRpcuXaiuro6UOKZMmcKjjz66x7oDDjiAjz76iNWrV7N69WqGDRvGrFmzGDo0GEHxjTfeYMCAAbHELllgBmefDf/4B5xxBtxwAxQVBW+W1yH5O1ZZWcm2bdsAWL9+PQsWLOBzn/scoO+YtExtKmkAjBo1ivnz59cuf+lLX+KMM85gzpw59OnTh2effRaAZcuW0bNnz4zqLi8v55RTTmnSeCUHdO8OpaUwaxYUFkI934vE79iKFSs4+uijGTx4MCNGjOCqq65i4MCBgL5j0jK1uZf7Jk2axNSpUznppJMA6rxUsHPnToYPH562rrlz5+6xPGvWLB5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YJz+eT+zsiTU5JQ1oFd69099tTbLoBGGpmSwluMp+XsO1B4BySLk0lmAh8y8xeA14nGGKzVnhJ7NPAwoQ43gI2hfdJ0pkGnGdmCwkuTdWchSwFdlkwWtzl9dRRc8xXCM60XgMeIXjaamOUfUUypV5uRVoBM8t39yoz60xwdlXi4RjrIk1J9zREWocZZnY40BG4XwlD4qIzDRERiUz3NEREJDIlDRERiUxJQ0REIlPSEBGRyJQ0REQksv8POiPfeuiHwlcAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.title(\"User Movie Preference\", size=15) \n",
    "plt.xlabel(\"Movie A rating\")\n",
    "plt.ylabel(\"Movie B rating\")\n",
    "plt.grid()\n",
    "\n",
    "# 绘制原始样本点，要求不同的影片喜好类别用不同的颜色标记\n",
    "# 提示: 用scatter散点图绘制，用它的参数c实现不同的类别用不同的颜色标记\n",
    "plt.scatter(X[y == 0][:, 0],X[y == 0][:, 1],c='blue',label='Action')\n",
    "plt.scatter(X[y == 1][:, 0],X[y == 1][:, 1],c='green',label='Comedy')\n",
    "\n",
    "# 绘制新数据点，用红色x标记，大小为8\n",
    "# 提示：用plt.plot()绘制，用它的参数marker实现不同的标记符号\n",
    "plt.plot(new_user[0, 0],new_user[0, 1],'rx',markersize=8)\n",
    "\n",
    "# 新数据最近邻索引为第一个最近邻的索引\n",
    "dist, idx = knn.kneighbors(new_user)\n",
    "nearest = X[idx[0][0]]# 获取最近邻点的坐标，这是一个列表，第一个元素是x坐标，第二个元素是y坐标\n",
    "\n",
    "# 用红线标记新数据点与最近邻点的连接线\n",
    "# 提示：用plt.plot()绘制，用 r-- 实现红色虚线\n",
    "plt.plot([new_user[0][0],nearest[0]],[new_user[0,1],nearest[1]],'r--')\n",
    "\n",
    "# 为每个点添加坐标文本  \n",
    "for x, y in zip(X[:, 0], X[:, 1]):\n",
    "    plt.text(x, y+0.1, f'({x}, {y})') \n",
    "\n",
    "# 为新数据点添加坐标文本\n",
    "plt.text(new_user[0,0], new_user[0,1]+0.1, f'({new_user[0,0]}, {new_user[0,1]})')\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
